<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/website/common/base.jsp"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>${webname }-采购模板</title>
<%@ include file="/website/common/common.jsp"%>
<link href="${webctx }/css/manage.css" rel="stylesheet" type="text/css" />
<link href="${webctx }/css/payment.css" rel="stylesheet" type="text/css" />
<style>
	.buy-btn a{ width:100px; height:22px; border:1px solid #ff6767; color:#ff6767; display:block; font-size:12px; padding-left:2px; text-indent:20px;line-height:22px;}
	.buy-btn i{ width:15px; height:16px; position:absolute; left:8px; top:4px; background:url(${webctx}/images/buyico.png) no-repeat;}
	.buy-btn a:hover{ color:#999; border:1px solid #999}
	.acc-btn{ width:120px; height:30px; line-height:30px;display:inline-block; font-size:15px; border-radius:2px; text-align:center;background:#fff; color:#23b0fc; border:1px solid #23b0fc}
	.acc-btn:hover{background:#14ABFC;color:#fff}
	.buy-btn3{margin-top:5px;}
	.buy-btn3 a{font-size:14px;}
	.th-info{width:560px;}
	.th-price{ width:150px;}
	.th-amount{ width:150px;}
	.td-pic{ width:140px;}
	.td-price{ width:150px;}
	.td-amount{ width:120px;}
	.td-amount .buy-Num-bd{ margin-top:20px; width:50px; height:28px; line-height:28px; padding:0 18px;}
	.td-amount .buy-Num-bd span{ width:24px; line-height:28px; height:28px;}
	.td-amount .buy-Num-bd input{ width:30px; height:22px; line-height:22px;padding-left:8px}
	.td-amount em{ padding:30px 0 0 45px; display:inline-block}
	.gray a{border:1px solid #999;color: #999;}
</style>
</head>

<body>
<%@ include file="/website/common/top.jsp"%>
<%@ include file="/website/common/head.jsp"%>
<%@ include file="/website/common/menu.jsp"%>

<div class="warp-body">
	<div class="inner clearfix">
    	<div class="left-main">
        	<div class="left-inner">
            	<%@ include file="/website/user/left.jsp"%>
            </div>
        </div>
    	<div class="right-main">
    		<input name="id" value="${bean.id}" type="hidden">
        	<div class="right-inner" style="background:#fff">
            	<div class="right-title"><span></span>采购模板</div>
            	<div class="line8"></div><div class="line8"></div>
            	<input name="panid" value="${bean.id }" type="hidden">
            	<div>
            		<span style="float:left;margin-left:10px">
            			<p style="padding:5px;font-size:16px;">${bean.name }</p>
            			<p style="padding:5px;color:#747474">${bean.explanation }</p>
            		</span>
            		<span style="float:right;padding-top:8px;"><a class="acc-btn marginR8" href="javascript:addsp(${bean.id });">添加商品</a><%--<a class="acc-btn marginR8" href="javascript:void(0);">采购此计划</a>--%></span>
            	</div>
            	<div class="line8 clear"></div><div class="line8"></div>
            	<!-- start -->
            	<div class="cart-th">
	            	<ul class="clearfix">

	            		<li class="th th-info"><div class="th-inner">商品信息</div></li>
	            		<li class="th th-price"><div class="th-inner">单价（元）</div></li>
	            		<li class="th th-amount"><div class="th-inner">数量</div></li>
	            		<li class="th th-op"><div class="th-inner">操作</div></li>
	            	</ul>
	            </div>
            	<div class="cart-bd">
            	<c:forEach items="${list }" var="product" varStatus="status">
	            	<div class="order-content" bid="${product.bid}" pid="${product.id }">
	                	<ul class="item-content clearfix">

	                        <li class="td td-pic">
	                        	<div class="td-inner" style="margin-left:10px;">
	                            	<a href="${ctx }/product/info?priceid=${product.id}" target="_blank"><img src="<%=getRandomFileserver()%>${product.photo}" width="90" height="90" alt="" onerror="this.src='${webctx }/images/noimg.jpg'"></a>
	                            </div>
	                        </li>
	                		<li class="td td-info">
	                        	<div class="td-inner">
	                            	<div class="info-title">${product.productname } ${product.name }</div>
	                                <ul class="info-txt">
	                                	<li class="clearfix">
	                                    	<span class="tl">生产厂家： ${product.B_chanjia }</span>
	                                        <span>件装量： ${product.B_zhuanl }</span>
	                                    </li>
	                                    <li class="clearfix">
	                                    	<span class="tl">规　　格： ${product.B_guige }</span>
	                                        <span>单　位： ${product.unitname }</span>
	                                    </li>
	                                    <li class="clearfix">
	                                    	<span class="tl">批准文号：${product.B_pzwh }</span>
	                                        <span>可拆零： ${product.B_kechai }</span>
	                                    </li>
	                                </ul>
	                            </div>
	                        </li>
	                		<li class="td td-price">
	                        	<div class="td-inner">
									<c:choose>
										<c:when test="${islogin && isuserauth }">
											<span style="display: block;">单价：￥${product.price }</span>
										</c:when>
										<c:when test="${!islogin }">
											<span style="display: block;">单价：登录可见</span>
										</c:when>
										<c:when test="${!isuserauth }">
											<span style="display: block;">单价：审核通过可见</span>
										</c:when>
									</c:choose>
	                                <span>库存：<c:choose><c:when test="${product.amount>100}">>100</c:when><c:when test="${product.amount<=0}">无</c:when><c:otherwise>${product.amount }</c:otherwise></c:choose></span>
	                            </div>
	                        </li>
	                        <li class="td td-amount">
	                        	<div class="td-inner">
	                                <div class="buy-Num-bd across">
	                                    <input name="buynumInp" type="text" class="buy-numInp" onchange="inputAmount(this)" value="${product.buyamount }" maxnum="${product.amount }"/>
	                                    <span class="btn-reduce" onclick="lessenamount(${product.id },this)">-</span>
	                                    <span class="btn-add" onclick="addamount(${product.id },this)">+</span>
	                                </div>
	                            </div>
	                        </li>
	                        <li class="td td-op">
	                        	<div class="td-inner">
	                            	<div>
										<c:if test="${product.status==2}">
	                            		<c:choose>
											<c:when test="${product.amount>0 && product.price>0}"><p class="buy-btn"><a href="javascript:addtocart(${product.id });"><i></i>加入购物车</a></p></c:when>
											<c:otherwise><p class="buy-btn gray"><a href="javascript:void(0);"><i></i>加入购物车</a></p></c:otherwise>
										</c:choose>
										</c:if>
	                                	<c:if test="${product.status==1 }"><p class="buy-btn2"><a style="padding-left:5px;padding-right:5px;font-size:14px;">已下架</a></p></c:if>
	                                	<p class="buy-btn3"><a href="javascript:deloften(${product.id });">删除此产品</a></p>
	                                </div>

	                            </div>
	                        </li>
	                	</ul>
	                </div>

	                </c:forEach>

	                ${pagehtml }
	            </div>
            	<!-- end -->
            </div>
        </div>
    </div>
</div>

<%@ include file="/website/common/foot.jsp"%>
</body>
</html>
<script>
var uploader;
$(function(){
	setLeftCur("cgmb");

})
function addsp(id){
	window.location.href=ctx+"/purchasetemp/addProductShow?panid="+id
}
function addtocart(pid){
	var url=ctx+"/order/addToCart?pid="+pid+"&amount="+$("[name='buynumInp']",$("[pid='"+pid+"']")).val();
//	window.open(url);
	$w.addtocart(url);
}
function query(){
	gopage(1);
}
function gopage(n){
	window.location.href=ctx+"/purchasetemp/show?pno="+n+"&id="+$("[name='id']").val();
}
function topageclick(){
	if($("[name='topagenum']").val()!="")
		gopage($("[name='topagenum']").val());
}
function deloften(pid){
	if(confirm("确定要移除？")){
		$w.httpRequest({
			url:apiroot+"/purchasetemp/delproduct",
			data:{"priceid":pid,"panid":$("[name='panid']").val()},
			ok:function(ret){
				$("[pid='"+pid+"']").remove();
			}
		})
	}
}
function addamount(pid,obj){
	 var txt=$("[name='buynumInp']",$(obj).parent());
	  var n=parseInt(txt.val());
	  var maxnum=0;
	  n++;
	  if(txt.attr("maxnum"))maxnum=parseInt(txt.attr("maxnum"));
	  if(n>maxnum)n=maxnum;
	  txt.val(n);
    inputAmount(txt);
}
function lessenamount(pid,obj){
	 var txt=$("[name='buynumInp']",$(obj).parent());
	  var n=parseInt(txt.val());
	  n--;
	  if(n<=0)n=1;
	  txt.val(n);
    inputAmount(txt);
}
function inputAmount(obj){
    $(obj).val($(obj).val().replace(/[^0-9]/g,''));
    if($(obj).val()=='0' || $(obj).val()=='')$(obj).val('1');
    if($(obj).attr("maxnum") && $(obj).val()){
        var max=parseInt($(obj).attr("maxnum"));
        if(parseInt($(obj).val())>max)$(obj).val(max);
    }
    synBuyCount($(obj).parents(".order-content").attr("bid"),$(obj).val()+'');
}
var synBuyCountCache = {};
var synBuyCountTimer = {};
function synBuyCount(cid,buycount){
    var buycountCache = synBuyCountCache[cid+'']+'';
    if(buycount==buycountCache){
        return true;
    } else {
        synBuyCountCache[cid+''] = buycount;
        var synBuyCountTimerPre = synBuyCountTimer[cid+''];
        if(synBuyCountTimerPre>0){
            window.clearTimeout(synBuyCountTimerPre);
            synBuyCountTimer[cid+''] = -1;
        }
        var synBuyCountTimerTemp = window.setTimeout(function(){
            synBuyCountTimer[cid+''] = -1;
            $w.httpRequest({
                url:"${ctx}/purchasetemp/updateProduct",
                data:{"priceId":cid+'' , "buyNum":buycount},
                ok:function(ret){

                }
            });
        } , 600 ) ;
        synBuyCountTimer[cid+''] = synBuyCountTimerTemp;
    }
}
var submiting=false;
function buyseled(){
	if($w.checkLogin(true)){
		if(submiting)return;
		submiting=true;
		$w.httpRequest({
			url:apiroot+"/order/generateorder2",
			data:{"type":"panbuy","data":${bean.id}},
			ok:function(ret){
				var ordercode=ret.Data;
				window.location.href=ctx+"/order/orderconfirm?ordercode="+ordercode;
			},fail:function(ret){
				if(ret.MsgCode=='1001')window.location.href=ctx+"/user/certification";
				else if(ret.MsgCode=='101'){
					var h="以下商品库存不足：";
					var list=window.eval(ret.MsgDesc);
					for(var i=0;i<list.length;i++){
						h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
					}
					alert(h);
				}else if(ret.MsgCode=='102'){
					var h="以下商品已下架：";
					var list=window.eval(ret.MsgDesc);
					for(var i=0;i<list.length;i++){
						h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
					}
					alert(h);
				}else if(ret.MsgCode=='103'){
					var h="以下商品已不在您的可购范围：";
					var list=window.eval(ret.MsgDesc);
					for(var i=0;i<list.length;i++){
						h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
					}
					alert(h);
				}else alert(ret.MsgDesc);
				return false;
			},all:function(){submiting=false;}
		})
	}
}
</script>